package com.ss.web.tagmark.init;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * 属性是集合的实体类处理器
 *
 * @author taofucheng
 *
 * @param <T>
 */
public class CollectionEntityEditor<T> extends PropertyEditorSupport {
	private JpaRepository<T, Long> repo;

	public CollectionEntityEditor(JpaRepository<T, Long> repo) {
		this.repo = repo;
	}

	public void setAsText(String text) throws java.lang.IllegalArgumentException {
		if (StringUtils.isBlank(text)) {
			return;
		}
		List<T> set = new ArrayList<>();
		for (String id : text.split(",")) {
			id = id.trim();
			if (StringUtils.isNumeric(id)) {
				set.add(repo.getOne(Long.parseLong(id)));
			}
		}
		super.setValue(set);
	}
}
